package mt.ctrl;

import base.jfinal.JsonUtil;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import base.ctrl.BaseController;
import base.jfinal.anatation.RouteBind;
import base.model.SessionModel;
import mt.Const;
import mt.business.Cache;
import mt.model.Shop;

import java.math.BigDecimal;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@RouteBind(path="shop")
public class ShopCtrl extends BaseController {

	public void findDemo() {

	}

	//后台用户登录
	public void login(){
		Record re;
		re=Db.findFirst("select shopid,loginid,password,shopName,status from mt_shop where loginid=?", this.getPara("loginId"));
		//不存在用户
		if(re==null){
			renderJson(Const.MSG_ERR_1);
		}else{
			if(Const.STATUS_NO==re.getInt("status")){
				this.renderJson(Const.MSG_ERR_3);
			}else if(!re.getStr("password").equals(this.getPara("password"))){
				this.renderJson(Const.MSG_ERR_2);
			}else{
				SessionModel sm=new SessionModel();
				sm.setLoginId(re.getStr("loginid"));
				sm.setShopName(re.getStr("shopName"));
				sm.setShopId(re.getInt("shopid"));
				this.setSessionAttr("SessionModel", sm);
				this.renderJson(Const.MSG_SUCCESS);
			}
		}

	}
    //获取店铺详情
    public void getDetail(){
        int shopId = ((SessionModel)this.getSessionAttr("SessionModel")).getShopId();
        Shop shop = Shop.dao.findById(shopId);
		JsonUtil result = new JsonUtil();
		result.put("shopname",shop.getStr("shopname"));
		result.put("province",shop.getStr("province"));
		result.put("city",shop.getStr("city"));
		result.put("area",shop.getStr("area"));
		result.put("street",shop.getStr("street"));
		result.put("phone",shop.getStr("phone"));
		result.putArray("photo", shop.getStr("photo"));
		this.renderJson(result.toString());
    }
    //获取收藏店铺
    public void getShop(){
    	String code = getPara("code", "");
        int userId = Cache.GetUserVOByCode(code).getUserId();        
            String selectUserSql ="SELECT muf.userid,muf.shopid,mu.shopname,mu.photo,mu.street,mu.area,mu.evalstar,mu.phone,mu.createDate,mu.cover FROM mt_user_favorites_shop muf,mt_shop mu WHERE mu.shopid=muf.shopid and muf.userid=?";
            List<Record> list = Db.find(selectUserSql,userId);
   	        this.renderJson((new JsonUtil(list)).toString());
   	    }
    //更新店铺详情
    public void updateDetail(){
        int shopId = ((SessionModel)this.getSessionAttr("SessionModel")).getShopId();
		String photo = this.getPara("photo", "[]");
		Shop shop = new Shop();
        shop.findById(shopId).setAttrs(this.getParas()).set("photo",photo).update();
        this.renderJson("{\"err\":0}");
    }

	public void getAppKey(){
        long now = System.currentTimeMillis();
        String appKey = SHA1("A6906507388832" + "UZ" + "A68C330D-FA53-6D2D-6559-F65C84D958E8" + "UZ" + now)+"."+now;
        this.renderJson("{\"key\":\""+appKey+"\"}");
	}

	public static String SHA1(String decript) {
		try {
			MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1");
			digest.update(decript.getBytes());
			byte messageDigest[] = digest.digest();
			// Create Hex String
			StringBuffer hexString = new StringBuffer();
			// 字节数组转换为 十六进制 数
			for (int i = 0; i < messageDigest.length; i++) {
				String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
				if (shaHex.length() < 2) {
					hexString.append(0);
				}
				hexString.append(shaHex);
			}
			return hexString.toString();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return "";
	}
    //用于APP调用
	public void getById(){
		Record record = getDBRecord("shop.getById", this.getParas());
		BigDecimal price = record.getBigDecimal("price");
		record.getColumns().put("displayprice", price != null ? price.divide(new BigDecimal(100)) : 0);
		JsonUtil result = new JsonUtil(record, "photo");
        this.renderJson(result.toString());
    }

}
